<?php
/**
 * Created by PhpStorm.
 * User: Jay
 * DateTime: 9/26/186:20 PM
 */

namespace AdminBundle\Services;


use AdminBundle\Controller\AdminController;
use AdminBundle\Model\m_admin;
use RedUnicorn\SymfonyKernel\Bundle\UnicornBundle\Services\Responses\Responses;
use RedUnicorn\SymfonyKernel\Model\Model;

class s_admin_auth extends AdminController
{
    /**
     * 登录
     *
     * @param $id
     * @return bool
     */
    private function login($id)
    {
        $m_admin = new m_admin($this->container);

        $data_login['last_login_ip'] = $this->get('unicorn.request')->getClientIp();
        $data_login['last_login_time'] = new \DateTime();
        $m_admin->update($id, $data_login);

        $this->setUserTag($id);

        return true;
    }

    /**
     * 登录检测
     *
     * @param $account
     * @param $password
     * @return array|\Symfony\Component\HttpFoundation\Response
     */
    public function loginAccountPWD($account,$password)
    {
        $m_admin= new m_admin($this->container);

        if (empty($account)){
            self::$error->setError('请输入帐号密码');
        }
        if(empty($password)){
            self::$error->setError('请输入密码');
        }
        if(self::$error->getError()){
            return Responses::arrays(self::$error->getError());
        }

        $info = $m_admin->getAssoc(
            [
                'select' => 'sql_pre.id, sql_pre.password, sql_pre.salt, sql_pre.is_disable ',
                Model::R_WHERE => ' AND sql_pre.account = "' . $account .'"'

            ]
        );

        $this->setUserTag($info['id']);

        if (empty($info)) {
            return Responses::arrays('帐号不存在');
        }

        $password_encode = $this->get('unicorn.string')->encryptPassword($password, $info['salt']);

        if ($password_encode != $info['password']) {
            return Responses::arrays('密码不正确');
        }

        if ($info['is_disable'] != 0) {
            return Responses::arrays('该用户已禁用');
        }
        $this->login($info['id']);

        return Responses::arrays('登录成功', 0);
    }

    /**
     * 入口检测
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     */
    public function inletCheck()
    {

        $m_admin = new m_admin($this->container);
        $user_tag=$this->getUserTag();


        if (empty($user_tag)){
            return Responses::arrays($this->get('translator')->trans('登录凭证失效'), $this->getParameter('error_code_not_logged'));
        }

        $info = $m_admin->getAssoc(
            [
                'select'=>'sql_pre.name,sql_pre.account,sql_pre.is_disable','id'=> $user_tag
            ]
        );

        if (empty($info)){
            return Responses::arrays($this->get('translator')->trans('该用户不存在'), $this->getParameter('error_code_not_logged'));
         }

        if ($info['is_disable'] != 0) {
            return Responses::arrays($this->get('translator')->trans('该用户已禁用'), $this->getParameter('error_code_not_logged'));
        }

        $this->setUserId($user_tag);
        self::$request->request->set('current_admin_name', $info['account']);

        return true;
    }

    /**
     * 修改密码
     *
     * @param $new_password
     * @param $user_id
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     */
    public function editPasswordByUserId ($new_password,$user_id){
        //参数验证
        if (empty($new_password) || empty($user_id)){
            return $this->response('参数错误');
        }

        //修改密码
        $m_admin = new m_admin($this->container);
        $data['password'] = $new_password;
        $res = $m_admin->update($user_id, $data);

        if ($res) {
            return true;
        }

        return false;
    }

    /**
     * 判断新旧密码正确性
     *
     * @param $new_password
     * @param $oldPassword
     * @param $user_id
     * @return bool
     */
    public function checkPWD($new_password,$oldPassword,$user_id)
    {
        //参数验证
        if (empty($oldPassword) || empty($new_password)){
            self::$error->setError('密码格式不正确');
        }

        //获取用户信息
        $m_admin = new m_admin($this->container);
        $user_info = $m_admin->getInfo($user_id);
        if(empty($user_info)){
            self::$error->setError('用户不存在');
        }
        $salt = $user_info['salt'];

        //判断旧密码正确性
        $encrypt_older_pwd = $this->get('unicorn.string')->encryptPassword($oldPassword, $salt);
        if ($encrypt_older_pwd != $user_info['password']){
            self::$error->setError('原始密码不正确');
        }

        //新旧密码不能完全一样
        $encrypt_new_pwd = $this->get('unicorn.string')->encryptPassword($new_password, $salt);
        if ($encrypt_new_pwd == $user_info['password']){
            self::$error->setError('原始密码与当前密码不能完全一致');
        }

        if (self::$error->getError()){
            return false;
        }

        return true;
    }
}